CentOS 7
Sponsored Link

SQL Server 2017 : SQL Server Agent
2017/10/08
 
Install SQL Server Agent to run scheduled jobs.
[1] Install SQL Server Agent.
[root@dlp ~]#
yum -y install mssql-server-agent
[root@dlp ~]#
systemctl restart mssql-server

[2] Add a test job and verify running.
[root@dlp ~]#
sqlcmd -S localhost -U SA

Password:

# create a test DB
1> create database TestDB_DailyJob; 
2> go 

1> select name,create_date from sys.databases; 
2> go 
name                             create_date
-------------------------------- -----------------------
master                           2003-04-08 09:13:36.390
tempdb                           2017-10-06 13:53:18.030
model                            2003-04-08 09:13:36.390
msdb                             2017-08-22 19:39:22.887
SampleDB                         2017-10-05 16:22:42.643
TestDB_DailyJob                  2017-10-06 14:10:16.500

(6 rows affected)

# switch to the System DB
1> use msdb; 
2> go 
Changed database context to 'msdb'.

# add a job name
1> exec dbo.sp_add_job @job_name = N'Daily Backup for TestDB'; 
2> go 

# add a job which takes backup of test DB
1> exec sp_add_jobstep 
2> @job_name = N'Daily Backup for TestDB', 
3> @step_name = N'Backup database', 
4> @subsystem = N'TSQL', 
5> @command = N'backup database TestDB_DailyJob to disk = \ 
6~ N''/var/opt/mssql/data/TestDB_DailyJob.bak'' with noformat, noinit, \ 
7~ name = ''TestDB-full'', skip, norewind, nounload, stats = 10', 
8> @retry_attempts = 5, 
9> @retry_interval = 5; 
10> go 

# add daily schedule (run at 23:50)
1> exec dbo.sp_add_schedule 
2> @schedule_name = N'Daily Backup for TestDB', 
3> @freq_type = 4, 
4> @freq_interval = 1, 
5> @active_start_time = 235000; 
6> go 
Changed database context to 'msdb'.

# attach a job to the schedule
1> exec sp_attach_schedule 
2> @job_name = N'Daily Backup for TestDB', 
3> @schedule_name = N'Daily Backup for TestDB'; 
4> go 

# add the schedule to local server
1> exec dbo.sp_add_jobserver 
2> @job_name = N'Daily Backup for TestDB', 
3> @server_name = N'(LOCAL)'; 
4> go 

# start job
1> exec dbo.sp_start_job N'Daily Backup for TestDB'; 
2> go 
Job 'Daily Backup for TestDB' started successfully.

# show job list
1> select * from sysjobschedules; 
2> go 
schedule_id job_id                               next_run_date next_run_time
----------- ------------------------------------ ------------- -------------
         10 C75EB787-B3D6-4242-915B-BF426AFB6871      20171006        235000

(1 rows affected)

1> select job_id,name from sysjobs; 
2> go 
job_id                               name
------------------------------------ ----------------------------
C75EB787-B3D6-4242-915B-BF426AFB6871 Daily Backup for TestDB

(1 rows affected)
[3] It's possible to make sure from SSMS on Windows Client.
 
Tweet